package com.xiaoyu.util2;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class EmailUtil {
	private Properties properties = new Properties();
	private static String CONFIG_FILE;
	private MimeMessage message;
	private Session session;
	private Transport transport;
	private String mailHost = "";
	private String sender_username = "";
	private String sender_password = "";

	public EmailUtil(boolean debug) {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(CONFIG_FILE));
			this.properties.load(in);
			this.mailHost = this.properties.getProperty("mail.smtp.host");
			this.sender_username = this.properties.getProperty("mail.sender.username");
			this.sender_password = this.properties.getProperty("mail.sender.password");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.session = Session.getInstance(this.properties);
		this.session.setDebug(debug);
		this.message = new MimeMessage(this.session);
	}

	public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser) {
		try {
			InternetAddress from = new InternetAddress(this.sender_username);
			this.message.setFrom(from);
			InternetAddress to = new InternetAddress(receiveUser);
			this.message.setRecipient(Message.RecipientType.TO, to);
			this.message.setSubject(subject);
			String content = sendHtml.toString();
			this.message.setContent(content, "text/html;charset=UTF-8");
			this.message.saveChanges();
			this.transport = this.session.getTransport("smtp");
			this.transport.connect(this.mailHost, this.sender_username, this.sender_password);
			this.transport.sendMessage(this.message, this.message.getAllRecipients());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (this.transport != null){
				try {
					this.transport.close();
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser, File attachment) {
		try {
			InternetAddress from = new InternetAddress(this.sender_username);
			this.message.setFrom(from);
			InternetAddress to = new InternetAddress(receiveUser);
			this.message.setRecipient(Message.RecipientType.TO, to);
			this.message.setSubject(subject);
			Multipart multipart = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
			multipart.addBodyPart(contentPart);
			if (attachment != null) {
				BodyPart attachmentBodyPart = new MimeBodyPart();
				DataSource source = new FileDataSource(attachment);
				attachmentBodyPart.setDataHandler(new DataHandler(source));
				attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
				multipart.addBodyPart(attachmentBodyPart);
			}
			this.message.setContent(multipart);
			this.message.saveChanges();
			this.transport = this.session.getTransport("smtp");
			this.transport.connect(this.mailHost, this.sender_username, this.sender_password);
			this.transport.sendMessage(this.message, this.message.getAllRecipients());
			System.out.println("send success!");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (this.transport != null){
				try {
					this.transport.close();
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
		}
	}

	static {
		URL path = EmailUtil.class.getProtectionDomain().getCodeSource().getLocation();
		CONFIG_FILE = path.getPath() + "mail.properties";
	}
}